#include <math.h>
#include "BarGraph.h"

BarGraph::BarGraph()
{
	Graph = NULL;
}
BarGraph::BarGraph(ColorPlane* Plane_)
{
	//Size_plane = Plane_->GetSize();
	//int Size1 = 0;
	//int k = 0;
	//if (Plane_ != NULL)
	//{	
	//	if (Plane_->GetSize() != 0)
	//	{		
	//		Size1 = Size = Plane_->GetSize();
	//	}

	//	Graph = new double[Size*2];
	//	for (int i = 0; i < Size*2; i+= 2)
	//	{
	//		if (Plane_->GetW() != 0)
	//		Graph[k] = Plane_->GetPixel((i/2)/Plane_->GetW(),(i/2)%Plane_->GetW());
	//		Graph[k+1] = 1;
	//		for (int j = 0; j < k; j+= 2)
	//		{
	//			if ((fabs(Graph[k] - Graph[j])<0.001)&&(k != j) )
	//			{
	//				Graph[j+1]++;					
	//				Graph[k+1]= 0;
	//				k-=2;
	//				j = k;
	//				Size1--;
	//			}
	//		
	//		}
	//		k+=2;
	//	}

	//}
	//Size = Size1;
	Size_plane = Plane_->GetSize();
	if (Plane_!= NULL)
	{
	double* Pixels = Plane_->GetPlaneAdress();
	int Size1 = 0;
	int k = 0;
	if (Pixels!= NULL)
	{
		Graph = new int[255];
		for (int i =0; i< 255; i++)
		{
			Graph[i] = 0;
		}
		for (int i =0; i<Size_plane; i++)
		{
			Graph[(int)(Pixels[i]*255)]++;
		}
		for (int i =0; i< 255; i++)
		{
			Graph[i] +=0;
		}

	}
	}
	//Sort_();
}
int* BarGraph::GetK(int &K)
{
	K = 0;
	for (int i = 1; i < 255; i++)
	{
		if (Graph[i-1]<Graph[i]&&Graph[i+1]>Graph[i])
			K++;
		//if (Graph[i-1]>Graph[i]&&Graph[i+1]>Graph[i])
		//	K++;
	}
	int* a = new int[K]; 
	int j = 0;
	for (int i = 1; i < 255; i++)
	{
		if (Graph[i-1]<Graph[i]&&Graph[i+1]>Graph[i])
		{
			if (abs(a[j-1]-i)>3)
			{			
				a[j]=i;
				j++;
			}

		}
		//if (Graph[i-1]>Graph[i]&&Graph[i+1]>Graph[i])
		//{
		//	a[j]=i;
		//	j++;
		//}
	}
	return a;
}
//void BarGraph::Create(ColorPlane* Plane_)
//{
//	if (Graph != NULL)
//	{
//		delete [] Graph;
//		Graph = NULL;
//	}
//	Size_plane = Plane_->GetSize();
//	int Size1 = 0;
//	int k = 0;
//	if (Plane_ != NULL)
//	{	
//		if (Plane_->GetSize() != 0)
//		{		
//			Size1 = Size = Plane_->GetSize();
//		}
//
//		Graph = new double[Size*2];
//		for (int i = 0; i < Size*2; i+= 2)
//		{
//			if (Plane_->GetW() != 0)
//			Graph[k] = Plane_->GetPixel((i/2)/Plane_->GetW(),(i/2)%Plane_->GetW());
//			Graph[k+1] = 1;
//			for (int j = 0; j < k; j+= 2)
//			{
//				if (fabs((Graph[k] - Graph[j]))<0.001&&(k != j) )
//				{
//					Graph[j+1]++;					
//					Graph[k+1]= 0;
//					k-=2;
//					j = k;
//					Size1--;
//				}
//			
//			}
//			k+=2;
//		}
//
//	}
//	Size = Size1;
//	Sort_();
//
//}

//void BarGraph::Sort_()
//{		
//	double temp_min = 0;
//	double next_min = 100000;
//	int next_min_i = 100000;
//	int temp = 0;
//	for (int i = 0; i < Size*2;i+=2 )
//	{
//		for (int j = i; j < Size*2; j+=2)
//		{
//			if (next_min != max(min(Graph[j],next_min),temp_min))
//			{
//				next_min = max(min(Graph[j],next_min),temp_min);
//				next_min_i = j;
//			}
//
//		}			
//		temp_min = Graph[next_min_i];
//		temp = Graph[next_min_i + 1];
//		Graph[next_min_i]=Graph[i];
//		Graph[next_min_i + 1] = Graph[i+1];
//		Graph[i] = temp_min;
//		Graph[i+1] = temp;
//		next_min = 100000;
//	}
//	FILE * vasya = fopen("1.txt","w");
//	for (int i = 0; i < Size*2; i+=2)
//	{
//		fprintf(vasya,"%lg  ",Graph[i+1]);
//	}
//	fclose(vasya);
//}
double BarGraph::GetEqualize(double temp)
{
	double vasya = 0;
	double temp_vasya =0;
	int i = 0;
	while (Graph[i] <= temp && i < Size * 2)
	{
			temp_vasya = Graph[i + 1];
			temp_vasya /= Size_plane;
			vasya += temp_vasya;
			i+=2;
	}
	return vasya;
}
double BarGraph::GetInvEq(double temp)
{
	double vasya = 0;
	double temp_vasya =0;
	double vasya_sum = 0;
	int i = 0;
	while (vasya_sum < temp && i < Size * 2)
	{
			temp_vasya = Graph[i + 1];
			temp_vasya /= Size_plane;
			vasya_sum += temp_vasya;
			vasya = Graph[i];
			i+=2;
	}
	return vasya;
}
BarGraph::~BarGraph()
{
	if (Graph != NULL)
	{
		delete[] Graph;
	}
}